package com.google.code.fluentjoiner;

import com.google.common.base.Predicate;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

public class PredicateUtils
{
    public static <T> Predicate<T> fromMatcher(final Matcher<T> matcher)
    {
        return new Predicate<T>()
        {
            @Override
            public boolean apply(final T input)
            {
                return matcher.matches(input);
            }
        };
    }

    public static <T> Matcher<T> fromPredicate(final Predicate<T> predicate)
    {
        return new TypeSafeMatcher<T>()
        {
            @Override
            public boolean matchesSafely(final T item)
            {
                return predicate.apply(item);
            }

            @Override
            public void describeTo(final Description description)
            {
                description.appendText("a value that doesn't match the predicate ").appendValue(predicate);
            }
        };
    }
}